package com.yt.controller;

import com.yt.entity.User;
import com.yt.loadbalance.MyLoadBalance;
import com.yt.loadbalance.impl.MyLoadBalanceImpl;
import com.yt.util.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname controller
 * @Description
 * @Date 2021/10/18 14:36
 * @Create by 杨涛
 */
@RestController
@Slf4j
public class UserController{
    @Resource
    RestTemplate restTemplate;
    @Resource
    MyLoadBalance myLoadBalance;
    //这里之前写死的端口，不适应集群的应用，修改为服务名
    public static final String pay_url="http://CLOUD-PAY-SERVICE";
            //"http://CLOUD-PAY-SERVICE";
    @Resource
    private DiscoveryClient discoveryClient;
    @RequestMapping("/getinfobylb")
    public String getinfoByLb(){
        List<ServiceInstance>  serviceInstanceList=discoveryClient.getInstances("CLOUD-PAY-SERVICE");
        if(serviceInstanceList== null || serviceInstanceList.size() <= 0){
            log.info("没有 provider 服务应用");
            return "provider 不存在";
        }

//        for(ServiceInstance serviceInstance:serviceInstanceList){
//            log.debug(serviceInstance.getUri() +" :" +serviceInstance.getHost());
//        }
        ServiceInstance serviceInstance= myLoadBalance.instance(serviceInstanceList);

        String uri=serviceInstance.getUri().toString();
        log.info("uri------->>>>"+serviceInstance.getUri().toString());
        String s=  restTemplate.getForObject(uri+"/getinfo",String.class);
        return "consumer get to provider--"+s;
    }

    @GetMapping("/getinfo")
    public String getinfobyget(){
        String s=  restTemplate.getForObject(pay_url+"/getinfo",String.class);
        return "consumer get to provider--"+s;
    }
    @PostMapping("/getinfo")
    public String getinfobypost(){
        ResponseEntity<String> entity=restTemplate.postForEntity(pay_url+"/getinfo",null,String.class);
        if(entity.getStatusCode().is2xxSuccessful())
            return "consumer post to  OK =》"+entity.getBody();
        else
            return "consumer post to    啥问题？？？"+entity.getStatusCode();
    }

//    public static final String pay_url="http://127.0.0.1:8001";
    @RequestMapping("/getusers")
    public CommonResult<User> getUser(){
        //get方式 ,可以不带参数，post方式中间是参数对像
        return restTemplate.getForObject(pay_url+"/getjsonusers",CommonResult.class);
    }
    @GetMapping("/adduser")
    public CommonResult<User> adduser(String username,String password){
        //使用Map时报错。应该使用如下类
        MultiValueMap<String,String> map=new LinkedMultiValueMap<>();

        map.add("username",username);
        map.add("password",password);
        return restTemplate.postForObject( pay_url+"/adduser",map,CommonResult.class);
    }
}
